package com.songcha.sakura.mvvm.local.video

import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.sakura.helper.VideoHelper
import com.songcha.sakura.struct.M3u8LocalFile
import com.songcha.sakura.struct.VideoType
import com.songcha.sakura.util.FileUtil
import com.songcha.sakura.util.VideoUtil
import java.io.File

class LocalVideoRepository : BaseRepository(){
    /*fun getLocalVideoList():MutableList<M3u8LocalFile>{
        val list:MutableList<M3u8LocalFile> = mutableListOf()

        //val dataPath=context.filesDir.absolutePath
        val dataPath= DownloadConfig.getVideoDownloadPath()
        val dataFile = File(dataPath)
        val files = dataFile.listFiles()
        for (file in files){
            if(file.isDirectory){

                var title=file.name

                val videoList= mutableListOf<M3u8LocalFile.LocalVideo>()
                val comicPath=dataPath+"/"+title+"/"
                val comicFile = File(comicPath)
                //番剧存在
                if(FileUtil.isDirExist(comicFile)){
                    //集存在
                    val files2 = comicFile.listFiles()
                    //遍历集
                    for (file2 in files2){
                        if(file2.isDirectory){
                            //var episodePath=file2.name
                            var episodeName=file2.name
                            if(episodeName.length>19){
                                episodeName=episodeName.substring(12,episodeName.length-".m3u8_0".length)
                            }

                            var fileSize=0
                            //遍历ts
                            val files2=file2.listFiles()
                            var tsCount=0
                            if(files2!=null){
                                for (ts in files2){
                                    if(ts.name.endsWith(".ts")){
                                        tsCount++
                                        fileSize+=ts.length().toInt()
                                    }

                                }
                            }

                            val url= FileUtil.readContent(file2.absolutePath+"/url.txt").split("|")[0]
                            val bean=VideoUtil.readPlayHistoryProgress(episodeName)

                            videoList.add(M3u8LocalFile.LocalVideo(url,title,file.absolutePath,file2.name,episodeName,file2.absolutePath,fileSize,
                                bean?.position?:0,bean?.duration?:0,tsCount,VideoType.M3U8))


                        }else if(file2.name.endsWith(".mp4")){
                            var episodeName=file2.name
                            if(episodeName.length>19){
                                episodeName=episodeName.substring(12,episodeName.length-".mp4".length)
                            }

                            val bean=VideoUtil.readPlayHistoryProgress(episodeName)
                            videoList.add(M3u8LocalFile.LocalVideo("",title,file.absolutePath,file2.name,episodeName,file2.absolutePath,file2.length().toInt(),
                                bean?.position?:0,bean?.duration?:0,0,VideoType.MP4))
                        }

                    }
                }

                list.add(0, M3u8LocalFile(title,file.absolutePath,videoList))
            }
        }
        return list
    }*/


    fun getLocalVideoList():MutableList<M3u8LocalFile>{
        val list:MutableList<M3u8LocalFile> = mutableListOf()
        lateinit var videoList:MutableList<M3u8LocalFile.LocalVideo>

        VideoHelper.getLocalVideoList(object:VideoHelper.ILocalVideoListener{
            override fun onSearchComicBefore(comicFile: File) {
                videoList=mutableListOf()
            }

            override fun onSearchEpisode(type:VideoType,comicFile: File, episodeFile: File) {
                when(type){
                    VideoType.M3U8->{
                        var episodeName=episodeFile.name
                        if(episodeName.length>18){
                            episodeName=episodeName.substring(11,episodeName.length-".m3u8_0".length)
                        }

                        var fileSize=0
                        //遍历ts
                        val files2=episodeFile.listFiles()
                        var tsCount=0
                        if(files2!=null){
                            for (ts in files2){
                                if(ts.name.endsWith(".ts")){
                                    tsCount++
                                    fileSize+=ts.length().toInt()
                                }

                            }
                        }

                        val url= FileUtil.readContent(episodeFile.absolutePath+"/url.txt").split("|")[0]
                        val bean=VideoUtil.readPlayHistoryProgress(episodeName)

                        videoList.add(M3u8LocalFile.LocalVideo(url,comicFile.name,comicFile.absolutePath,episodeFile.name,episodeName,episodeFile.absolutePath,fileSize,
                            bean?.position?:0,bean?.duration?:0,tsCount,type))
                    }
                    VideoType.MKV,VideoType.MP4->{
                        var episodeName=episodeFile.name
                        if(episodeName.length>18){
                            episodeName=episodeName.substring(11,episodeName.length-4)
                        }
                        val bean=VideoUtil.readPlayHistoryProgress(episodeName)
                        videoList.add(M3u8LocalFile.LocalVideo("",comicFile.name,comicFile.absolutePath,episodeFile.name,episodeName,episodeFile.absolutePath,episodeFile.length().toInt(),
                            bean?.position?:0,bean?.duration?:0,0,type))
                    }
                    else -> {}
                }
            }

            override fun onSearchComicAfter(comicFile: File) {
                list.add(0, M3u8LocalFile(comicFile.name,comicFile.absolutePath,videoList))
            }
        })
        return list
    }
}